package penguin.masking;

/**
 * 优先级打印
 *
 * @author wuxh
 * @version 1.0.0
 */
public class Logger {

    public static String level = LEVEL.ERROR.name;

    private Logger() {

    }

    public static void debug(String message) {
        if (isDebug()) {
            System.out.println("masking:DEBUG " + message);
        }
    }

    public static boolean isDebug() {
        return match(LEVEL.DEBUG);
    }

    public static void debug(String message, Throwable throwable) {
        if (match(LEVEL.DEBUG)) {
            System.out.println("masking:DEBUG " + message);
            print(throwable);
        }
    }

    public static void info(String message) {
        if (match(LEVEL.INFO)) {
            System.out.println("masking:INFO " + message);
        }
    }

    public static void info(String message, Throwable throwable) {
        if (match(LEVEL.INFO)) {
            System.out.println("masking:INFO " + message);
            print(throwable);
        }
    }

    public static void warn(String msg) {
        if (match(LEVEL.WARN)) {
            System.err.println("masking:WARN " + msg);
        }
    }

    public static void warn(String msg, Throwable throwable) {
        if (match(LEVEL.WARN)) {
            System.err.println("masking:WARN " + msg);
            print(throwable);
        }
    }

    public static void error(String msg) {
        if (match(LEVEL.ERROR)) {
            System.err.println("masking:ERROR " + msg);
        }
    }

    public static void error(String msg, Throwable throwable) {
        if (match(LEVEL.ERROR)) {
            System.err.println("masking:ERROR " + msg);
            print(throwable);
        }
    }

    private static void print(Throwable throwable) {
        if (throwable != null) throwable.printStackTrace();
    }

    private static boolean match(LEVEL l) {
        if (Logger.level == null) {
            level = LEVEL.ERROR.name;
        }
        LEVEL level = LEVEL.valueOf(Logger.level);
        if (level == null) {
            return false;
        }
        //按优先级打印
        if (level.priority <= l.priority) {
            return true;
        }

        return false;
    }

    enum LEVEL {
        DEBUG("DEBUG", 1), WARN("WARN", 2), INFO("INFO", 3), ERROR("ERROR", 4);

        private final String name;
        private final int priority;

        LEVEL(String name, int priority) {
            this.name = name;
            this.priority = priority;
        }
    }
}
